package user;

import book.Book;
import book.Library;
import utils.SingletonScanner;

import java.time.LocalDate;
import java.util.Scanner;

public class AdminUser extends User{

    private Library library = Library.getInstance();
    private Scanner scanner = SingletonScanner.getScanner();
    public AdminUser(int id, String name){
        super(id, name, "管理员");
    }

    @Override
    public int display() {
        System.out.println("管理员用户:"+this.name+"的菜单");
        System.out.println("1. 查找书籍");
        System.out.println("2. 显示书籍");
        System.out.println("3. 退出系统");
        System.out.println("4. 上架书籍");
        System.out.println("5. 更新书籍");
        System.out.println("6. 删除书籍");
        System.out.println("7. 查看受欢迎的前k本书");
        System.out.println("8. 统计书籍的借阅次数");
        System.out.println("9. 查看库存状态");
        System.out.println("10. 检查超过一年未下架的书籍");

        System.out.println("请输入你的选择:");
        int choice = scanner.nextInt();
        scanner.nextLine();

        return choice;
    }

    public void addBook() {
        System.out.println("管理员的addBook执行了");

        System.out.println("请输入书籍的名称: ");
        String title = scanner.nextLine();

        System.out.println("请输入书籍的作者: ");
        String author = scanner.nextLine();

        System.out.println("请输入书籍的出版时间: ");
        int publishYear = scanner.nextInt();

        scanner.nextLine();
        System.out.println("请输入书籍的类别: ");
        String category = scanner.nextLine();

        LocalDate shelfDate = LocalDate.now();
        Book book = new Book(title, author, shelfDate, publishYear, category);
        library.addBook(book);
    }

    public void updateBook(){
        System.out.println("管理员的updateBook执行了");
        library.dispalyBook();
        System.out.println("请输入你要修改的书名id: ");
        int bookId = scanner.nextInt();
        Book oldBook = library.searchBookByBid(bookId);
        if(oldBook == null){
            System.out.println("没有书名id为 " + oldBook.getId() + " 的书籍。" );
        }else {
            System.out.println("旧书籍的名称:" + oldBook.getTitle());
            System.out.println("请输入新书籍的名称: ");
            scanner.nextLine();

            String title = scanner.nextLine();

            System.out.println("旧书籍的作者: " + oldBook.getAuthor());
            System.out.println("请输入新书籍的作者: ");
            String author = scanner.nextLine();

            System.out.println("旧书籍的出版时间: " + oldBook.getPublishYear());
            System.out.println("请输入书籍的出版时间: ");
            int publishYear = scanner.nextInt();

            scanner.nextLine();
            System.out.println("旧书籍的类别: " + oldBook.getCategory());
            System.out.println("请输入书籍的类别: ");
            String category = scanner.nextLine();

            LocalDate shelfDate = LocalDate.now();

            Book newBook = new Book(title, author, shelfDate, publishYear, category);
            newBook.setId(oldBook.getId());

            library.updateBook(newBook);
        }
    }

    public void removeBook() {
        System.out.println("管理员的removeBook执行了");
        library.dispalyBook();
        System.out.println("请输入你要删除的书名id: ");
        int bookId = scanner.nextInt();
        Book book = library.searchBookByBid(bookId);
        if(book == null){
            System.out.println("没有书名id为 " + book.getId() + " 的书籍。" );
        }
        library.removeBook(book);
        System.out.println("删除成功!");
    }

    public void checkAndRemoveOldBook() {
        System.out.println("管理员的checkAndRemoveOldBook执行了");
        library.checkAndRemoveOldBook();
    }

    public void checkInvertoryStatus() {
        System.out.println("管理员的checkInvertoryStatus执行了");
        library.checkInvertoryStatus();
    }

    public void generateBook() {
        System.out.println("管理员的generateBook执行了");
        library.dispalyBook();
        System.out.println("请输入你要查看的最受欢迎的前k本书: ");
        int k = scanner.nextInt();
        if(k <= 0 || k >= library.getBorrowCount()){
            System.out.println("没有最受欢迎的前 " + k + " 本书");
            return;
        }
        library.generateBook(k);
    }

    public void BorrowedCount() {
        System.out.println("管理员的BorrowedCount执行了");
        library.BorrowedCount();
    }
}
